package net.sxnic.item.udcp;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import net.sxinfo.core.dao.hibernate3.HibernateCriteria;
import net.sxinfo.core.spring25.Dao;
import net.sxinfo.core.spring25.DefaultManager;
import net.sxnic.comm.utils.CommUtils;
import net.sxnic.item.catalog.Catalog;
import net.sxnic.item.catalog.CatalogManager;
import net.sxnic.item.clpv.Clpv;
import net.sxnic.item.ucenter.UcenterService;
import net.sxnic.item.udcp.dao.UdcpDao;
import net.sxnic.ugr.dept.Dept;
import net.sxnic.ugr.user.User;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("udcpManager")
public class DefaultUdcpManager extends DefaultManager<Udcp, UdcpDao, String>
		implements UdcpManager {

	@Autowired
	private UdcpDao udcpDao;
	
	@Autowired
	private UcenterService ucService;
	
	@Autowired
	private CatalogManager clManager;
	
	@Override
	protected Dao<Udcp, String> getEntityDao() {
		return udcpDao;
	}

	@Override
	public Map<String, String[]> findAllCatalogsByUserNameCate(String userName,
			String cate) {
		if(!ArrayUtils.contains(Clpv.DEFAULT_PERMISSIONS, cate)){
			return null;
		}
		
		Map<String, String[]> map = new HashMap<String, String[]>();
		
		
		HibernateCriteria hc = new HibernateCriteria();
		Criterion dc = null;
		
		//先找用户定义的大类 小类
		User user = ucService.findUserByUserName(userName);
		
		//先找用户所在单位定义的大类 小类
		Dept dept = user.getDept();
		
		dc = Restrictions.eq("userName", userName);
		Criterion dc1 = Restrictions.eq("deptCode", dept.getCode());
		
		dc = Restrictions.or(dc, dc1);
		
		dc1 = Restrictions.eq("deptCode", dept.getParent().getCode());
		
		dc = Restrictions.or(dc, dc1);
		
		List<Udcp> list = udcpDao.getByCriteria(hc);
		List<Catalog> cList = clManager.getAll();
		
		String temps = "";
		for(Udcp u:list){
			if(StringUtils.isBlank(temps)){
				temps = u.getClpvName();
			}else{
				temps = temps+","+u.getClpvName();
			}
		}
		
		Set<String> catas = null;
		for(Catalog c:cList){
			catas = new LinkedHashSet<String>();
			List<Catalog> c2List = clManager.findCatalog2ByCatalog1(c.getAlias());
			for(Catalog c2:c2List){
				if(StringUtils.contains(temps, c.getAlias()+"."+c2.getCode()+"_"+cate)){
					catas.add(c2.getCode());
				}
			}
			
			if(catas.size()>1){
				map.put(c.getAlias(), CommUtils.SettoArray(catas));
			}
		}
		
		
		return map;
	}

	@Override
	public String findLinkedCatalogScriptByUserNameCate(String userName,
			String cate) {
		
		Map<String, String[]> map = findAllCatalogsByUserNameCate(userName, cate);
		
		
		StringBuffer sb = new StringBuffer();

		if (map.size() > 0) {
			Iterator<Entry<String, String[]>> iter = map.entrySet().iterator();
			// 生成第一层的数据
			sb.append("{'0':{");
			int i = 0;
			while (iter.hasNext()) {
				i++;
				Entry<String, String[]> object = iter.next();
				if (i == 1) {
					sb.append("'" + object.getKey().toString() + "':");
					sb.append("'" + object.getValue().toString() + "'");
				} else {
					sb.append(",'" + object.getKey().toString() + "':");
					sb.append("'" + object.getValue().toString() + "'");
				}

			}
			sb.append("},");

			// String cyear = CommConstant.PROPERTY_MAP.get("zb.optYear");
			// 生成第二层的数据
			iter = map.entrySet().iterator();
			i = 0;
			while (iter.hasNext()) {
				i++;
				Entry<String, String[]> object = iter.next();
				if (i == 1)
					sb.append("'0," + object.getKey().toString() + "':{");
				else
					sb.append(",'0," + object.getKey().toString() + "':{");
				// 根据被考核单位类别获得被考核单位列表
				List<Catalog> dList = clManager.findCatalog2ByCatalog1(object.getKey()
						.toString());
				int j = 0;
				for (Catalog d : dList) {
					j++;
					if (j == 1)
						sb.append("'" + d.getAlias() + "':");
					else
						sb.append(",'" + d.getAlias() + "':");
					sb.append("'" + d.getName() + "'");
				}

				sb.append("}");
			}
			sb.append("}");
		}

		return sb.toString();
	}

}
